<?php

/**
 * 批量更新 wiki详情页 住友说、品牌说、门店说计数器
 */

error_reporting(E_ALL ^ E_NOTICE ^ E_WARNING ^ E_DEPRECATED);
ini_set('display_errors', 1);
require_once '/data/wwwroot/g_vendor/autoload.php';

use Biz\model\wiki\Wiki as mWiki;
use Biz\service\wiki\ObjRelateWiki;


$taskId = (int)$argv[1] ?? 0;

$programStartTime = time();

$maxWikiId = mWiki::getByWhere(['status[!]' => mWiki::STATUS_DEL, "ORDER" => ['id' => 'DESC'], 'LIMIT' => 1], 'id');
$pageSize = 4000;

$page = ceil($maxWikiId / $pageSize);

$i = 0;
while ($i < $page) {
    $start = $i * $pageSize;
    $end = ($i + 1) * $pageSize;
    //获取所有正常的wiki
    $wikiIds = mWiki::selectByWhere(['status[!]' => mWiki::STATUS_DEL, "id[<>]" => [$start, $end]], ['id', 'basic_id']);
    if ($wikiIds) {
        $j = 0;
        foreach ($wikiIds as $wikiInfo) {
            $wikiId = $wikiInfo['id'];
            $basicId = $wikiInfo['basic_id'];
            if ($wikiId % 10 == $taskId) {
                $j++;
                //更新计数器
                $isSync = ObjRelateWiki::syncWikiCommentCounter($wikiId, $basicId);
                if(!$isSync){
                    echo '计数器更新失败'.'$wikiId:'.$wikiId.'$basicId:'.$basicId.PHP_EOL;
                }

            }
        }
        echo '第'.$i.'批数据执行完成,共修改'.$j.'条数据'.PHP_EOL;
    }
    $i++;
}
$programEndTime = time();
echo 'taskId:'.$taskId.'共消耗时间'.($programEndTime - $programStartTime).PHP_EOL;
